package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @Classname SetOrderToGoodAttrValueAttrHandler
 * @Description 部分商品属性，同时存在于产品、订单模块，订单属性赋值值，值复制给产品属性
 * @Author zhang.song
 * @Date 2021-03-29 11:18
 */
@Service
public class SetOrderToGoodAttrValueAttrHandler extends AbstractAttrHandler {

    @Resource
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Autowired
    private IOperateSceneInstService operateSceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        String serviceOfferId = Optional.ofNullable(querySceneInstService.getSceneInst(sceneInstId)).map(KiteSceneInst::getServiceOfferId).orElse("");
        if(KiteStringUtils.isEqual(serviceOfferId, "2826")) {
            if(KiteStringUtils.isEmpty(commonAttr.getValue())) {
                String fieldName = getGoodFileName(commonAttr.getFieldName());
                if(KiteStringUtils.isNotEmpty(fieldName)) {
                    CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
                    // 查询这个offer下有效的接入产品
                    List<KiteInst> prodInstList = querySceneInstService.listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST));
                    prodInstList.forEach(prodInst ->{
                        KiteAttr prodAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), fieldName);
                        if (Objects.nonNull(prodAttr) && KiteStringUtils.isNotEmpty(prodAttr.getValue())) {
                            String value = getAttrValue(commonAttr.getFieldName(), prodAttr.getValue());
                            commonAttr.setValue(value);
                            commonAttr.setValueDesc(prodAttr.getValueDesc());
                            commonAttr.setOriginValue(value);

                            KiteAttr kiteAttr = querySceneInstService.getCommonAttr(sceneInstId, commonAttr.getFieldName());
                            kiteAttr.setValue(value);
                            kiteAttr.setValueDesc(prodAttr.getValueDesc());
                            kiteAttr.setOriginValue(value);
                            operateSceneInstService.setCommonAttr(sceneInstId, kiteAttr);
                        }
                    });
                }
            }
        }
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if (KiteStringUtils.isNotEmpty(value)) {
            this.setGoodAttrValue(sceneInstId, fieldName, value);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

    /**
     * 设置对应的产品属性值
     */
    public void setGoodAttrValue(String sceneInstId, String fieldName, String commonAttrValue) {
        String goodFieldName = getGoodFileName(fieldName);
        String value = getAttrValue(goodFieldName, commonAttrValue);
        CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        //查询这个offer下有效的接入产品
        List<KiteInst> prodInstList = querySceneInstService.listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST));
        prodInstList.forEach(prodInst ->{
            //分配对象单位ID
            KiteAttr prodAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), goodFieldName);
            if (Objects.nonNull(prodAttr) && !KiteStringUtils.isEqual(prodAttr.getValue(), value)) {
                operateInstAttrService.setAttrValue(prodInst, prodAttr.getFieldName(), value, sceneInstId, null);
            }
        });
    }

    /**
     * 根据订单attrField，获取产品属性fileName（此产品属性的值，需要等于订单属性填写值）
     */
    private static String getGoodFileName(String attrField) {
        String fileName;
        switch (attrField) {
            case "ORD10056":
                // 组网名称
                fileName = "200001638";
                break;
            case "ORD10057":
                // 开始时间
                fileName = "200000247";
                break;
            case "ORD10058":
                // 接入时长
                fileName = "200001639";
                break;
            default:
                fileName = null;
                break;
        }
        return fileName;
    }

    private static String getAttrValue(String fileName, String value) {
        if(KiteStringUtils.isEqual(fileName, "ORD10058")) {
            if(KiteStringUtils.isEqual(value, "100000420")) {
                return "1";
            } else if(KiteStringUtils.isEqual(value, "100000421")) {
                return "2";
            } else if(KiteStringUtils.isEqual(value, "100000422")) {
                return "3";
            }
        } else if(KiteStringUtils.isEqual(fileName, "200001639")) {
            if(KiteStringUtils.isEqual(value, "1")) {
                return "100000420";
            } else if(KiteStringUtils.isEqual(value, "2")) {
                return "100000421";
            } else if(KiteStringUtils.isEqual(value, "3")) {
                return "100000422";
            }
        }
        return value;
    }
}
